﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TeacherHelp.DAL;

namespace TeacherHelp.BLL
{
    public class SubjectsManager
    {
        private readonly SubjectsService _subjectService;

        public SubjectsManager()
        {
            _subjectService = new DAL.SubjectsService();
        }

        public string AddSubject(Model.Subjects subjects)
        {
            string erroInfo = null;
            try
            {
                Log.Info("添加科目中..");
                var sql = _subjectService.GetSubjectCountSQL(subjects.Name);
                if (DBHelper.ExecuteScalar(sql).ToInt() > 0)
                {
                    erroInfo = "科目已存在！";
                    Log.Info(erroInfo);
                    return erroInfo;
                }

                sql = _subjectService.GetInsertSubjectSQL(subjects.Name);
                var result = DBHelper.ExecuteNonQuery(sql);
                if (result < 1)
                {
                    erroInfo = "添加科目失败！";
                    Log.Info(erroInfo + " result=" + result);
                }
                else
                {
                    Log.Info("添加科目成功！");
                }
            }
            catch (Exception ex)
            {
                Log.Error("添加科目时出现异常！", ex);
            }
            return erroInfo;
        }

        public string EditSubject(Model.Subjects subjects)
        {
            string erroInfo = null;
            try
            {
                Log.Info("编辑科目中..");
                var sql = _subjectService.GetUpdateSubjectSQL(subjects);
                var result = DBHelper.ExecuteNonQuery(sql);
                if (result < 1)
                {
                    erroInfo = "编辑科目失败";
                }
                else
                {
                    Log.Info("编辑科目成功！");
                }
            }
            catch (Exception ex)
            {
                Log.Error("编辑科目时出现异常！", ex);
            }
            return erroInfo;
        }

        public string RemoveSubject(string id)
        {
            string erroInfo = null;
            try
            {
                Log.Info("删除科目中..");
                var sql = _subjectService.GetDelSubjectSQL(id);
                var result = DBHelper.ExecuteNonQuery(sql);
                if (result < 1)
                {
                    erroInfo = "删除科目失败";
                }
                else
                {
                    Log.Info("删除科目成功！");
                }
            }
            catch (Exception ex)
            {
                Log.Error("删除科目时出现异常！", ex);
            }
            return erroInfo;
        }

        public List<Model.Subjects> GetSubejctAll()
        {
            var sql = _subjectService.GetSubjectsInfoSQL();

            try
            {
                Log.Info("获取所有科目");
                var result = DBHelper.GetListModel<Model.Subjects>(sql);
                return result;
            }
            catch (Exception ex)
            {
                Log.Error("获取全部科目时出现异常！", ex);
                return default;
            }
        }

        public List<Model.Subjects> GetSubjectsCount50()
        {
            var sql = _subjectService.GetSubjectsCount50SQL();
            try
            {
                Log.Info("获取大于等于50道题目的科目");
                var result = DBHelper.GetListModel<Model.Subjects>(sql);
                return result;
            }
            catch (Exception ex)
            {
                Log.Error("获取大于等于50道题目的科目时出现异常！", ex);
                return default;
            }
        }
    }
}
